layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;


    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 340
         ,url:"/admin/getButieType"
         ,cellMinWidth: 80
         ,cols: [[
             {field:'butieName', title: '补贴名称',width: 180}
             ,{field:'money', width: 180   , title: '补贴金额'}
             ,{field:'userd',width: 180   ,  title: '是否启用',templet:"#payTem"}
             ,{field:'',width:370,title: '操作',toolbar: '#barDemo'}
         ]]
         ,page:true
     });


     //监听工具条
     table.on('tool(dataTable)',function (obj) {
         var data = obj.data;
         if(obj.event == 'detail'){
             //查看，修改
             parent.layui.element.tabAdd('tabs',{
                 title:"查看和修改",
                 content:"<iframe  src='/admin/toButieTypeEdit?butieName="+data.butieName+"'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
                 id:"butieTypeEdit"
             });
             rightMenu();
             iframeWH();
             parent.layui.element.tabChange('tabs','butieTypeEdit');

         }else if(obj.event == 'del'){
             //禁用
             $.ajax({
                 type:"POST"
                 ,data:{
                     butieName:data.butieName
                 }
                 ,url:"/admin/deleteButieType"
                 ,success:function (res) {
                     layer.msg(res.message);
                 }
                 ,error:function () {
                     layer.msg("系统错误！");
                 }
             });
         }else if(obj.event == 'send'){
             //发送账单
             layer.confirm('是否向所有司机发送补贴？', {
                 btn: ['确定'] //按钮
             }, function(){
                    $.ajax({
                        type:"POST"
                        ,url:"/admin/sendButie"
                        ,data:{
                            "butieName":data.butieName
                        }
                        ,success:function (res) {
                            layer.msg(res.message);
                        }
                        ,error:function () {
                            layer.msg("系统错误！");
                        }
                    });
             });
         }else  if(obj.event == 'sendSington'){

             //单个发送
             layer.open({
                 type: 2,
                 title: '选择司机',
                 shadeClose: true,
                 shade: 0.8,
                 area: ['400px', '400px'],
                 content: '/admin/toDriverListView2?butieName='+data.butieName //iframe的url
             });
         }
     });


    $("#payBtn").bind("click",function () {
        alert("hello");
    })

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                butieName:$("#butieName").val()
            }
        })
    });;

    $("#add").on("click",function () {

        parent.layui.element.tabAdd('tabs',{
            title:"新增补贴",
            content:"<iframe  src='/admin/butieTypeToAdd'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
            id:"butieTypeAdd"
        });
        rightMenu();
        iframeWH();
        parent.layui.element.tabChange('tabs','butieTypeAdd');


    })


});
